﻿--[[
   @brief    GroupBannerList
   @author   vincentlao
   @version  1.0
   @date     2015-06-11
--]]

group_guide = {
    no_group_guider_ctrl_ = nil;
    group_list_ctrl_ = nil;
    root_id_ = 0;
    uilib_ = nil;
    group_id_to_btn_id_ = {};
};

function group_guide:log(msg, core_msg)
    base_utility.log(string.format("[group_guide] %s", msg), core_msg);
end

function group_guide:attach(uilib, ctrl_id)
    self:log(string.format("attach %d", ctrl_id));
    self.uilib_ = uilib;
    self.root_id_ = ctrl_id;
    self.no_group_guider_ctrl_ = ui_utility.get_ctrl_ptr_by_id(ctrl_id);

    local str, str_holder = U8toCUIStr("TinyContainer_GroupRecommend\\Container_1\\Ctn_GroupList\\DraggableList_PushGroup");
    self.group_list_ctrl_ =ui_utility.get_ctrl_ptr_by_id(uilib:GetControlIDbyPath(str, ctrl_id));
end

function group_guide:detach()
    self:log("detach");
    self.no_group_guider_ctrl_ = nil;
end

function group_guide:show(show)
    if self.no_group_guider_ctrl_ ~= nil then
        self.no_group_guider_ctrl_:Show(show);

        if show == TRUE then
            self:adjust_view();


            group_guide:update_group_info(group_list_model.get_recommend_group());
        end
    end

    if show == TRUE then
        self:get_recommend_group();
    end
end

function group_guide:adjust_view()
    local data = comm_center.call_service('i_service_social_mainwnd_is_shown', {});    

    if data.is_show == 1 then
        if self.is_max_wnd_adjust == nil then
            self.is_max_wnd_adjust = true;

            local ui_str, str_holder = U8toCUIStr("TinyContainer_GroupRecommend\\Container_1");
            local parent_id = self.uilib_:GetControlIDbyPath(ui_str, self.root_id_);
            local parent_ptr = ui_utility.get_ctrl_ptr_by_id(parent_id);
            local parent_rc = parent_ptr:GetRectEx(FALSE,TRUE);
            parent_rc.bottom = parent_rc.bottom + 300;
            parent_ptr:MoveEx(parent_rc,TRUE);


            ui_str, str_holder = U8toCUIStr("TinyContainer_GroupRecommend\\Container_1\\Ctn_GroupList");
            local group_list_id = self.uilib_:GetControlIDbyPath(ui_str, self.root_id_);
            ui_utility.setproperty_by_id(group_list_id, "Visible", "true");

            ui_str, str_holder = U8toCUIStr("TinyContainer_GroupRecommend\\Container_1\\Ctn_ZBtn");
            local btn_id = self.uilib_:GetControlIDbyPath(ui_str, self.root_id_);
            local btn_ptr = ui_utility.get_ctrl_ptr_by_id(btn_id);
            local btn_rc = btn_ptr:GetRectEx(FALSE,TRUE);

            local h = btn_rc.bottom - btn_rc.top;
            btn_rc.top = btn_rc.top + 370;
            btn_rc.bottom = btn_rc.top + h;

            btn_ptr:MoveEx(btn_rc,TRUE);
        else
            return;
        end
    else
        return;
    end
end

function group_guide:get_recommend_group() --获取推荐群组
    self:log("get_recommend_group");
    local social_network_model =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    if social_network_model then
        local tpf_ui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        tpf_ui:AddLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_GET_RECOMMENT_GROUP_LIST, "group_guide:get_recommend_group_rsp");
        social_network_model:get_recommend_group();
    end
end

function group_guide:get_recommend_group_rsp(event_id, json_str)
    local tpf_ui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpf_ui:RemoveLuaListener(ierd_tgp.social_network.SOCIAL_NETWORK_EVENT_GET_RECOMMENT_GROUP_LIST, "group_guide:get_recommend_group_rsp");
    local group_tbl = base_utility.json_to_table(json_str);
    base_utility.log("[group_guide][get_recommend_group_rsp], result:"..group_tbl["result"]..", count:"..group_tbl["count"] )
    if group_tbl and group_tbl["result"] == 0 and group_tbl["count"] > 0 then
        group_list_model.set_recommend_group(group_tbl["groups"]);
        group_guide:update_group_info(group_tbl["groups"]);
    end
end

function group_guide:click_on_change_group(uilib, ctrl_id)
    self:log("click_on_change_group");
    group_guide:get_recommend_group();
end

function group_guide:update_group_info(group_tbl)
    if group_tbl == nil then
        return;
    end

    self:log("update_group_info");
    self:remove_all_group();
    self.group_id_to_btn_id_ = {};
    for index, group in ipairs(group_tbl) do
        group_guide:insert_one_group(index - 1, group);
    end
end

function group_guide:insert_one_group(index, group)
    if self.group_list_ctrl_ == nil then
        return;
    end

    local ui_str, str_holder = U8toCUIStr("social\\group_banner_for_recommend.tpf");
    local group_banner_id = self.group_list_ctrl_:InsertItem(0, ui_str);
    self.group_list_ctrl_:SetItemDraggable(0, false);

    local id = self:get_sub_element_id("Button_Head_Image", group_banner_id);
    ui_utility.setproperty_by_id(id, "Background", string.format("ONEIMAGE:social_network\\group-head-small.png|%s;Circle;null;", group["face_url"]));
    
    local game_info = banner_model.get_game_base_info(26);
    if game_info ~= nil then
        local game_image_url = string.format("ONEIMAGE:social_network\\game.png|%s;Stretch;null;", game_info.icon_url);
        id = self:get_sub_element_id("TContainer_Game_Image", group_banner_id);
       ui_utility.setproperty_by_id(id, "Background", game_image_url);
    end
    
    id = self:get_sub_element_id("Button_Group_Name", group_banner_id);
    ui_utility.setproperty_by_id(id, "Caption", group["name"]);

    id =self:get_sub_element_id("Static_Game_Server", group_banner_id);

    ui_utility.setproperty_by_id(id, "Caption", string.format(LoadStr("SOCIAL_GROUP_LIST_GROUP_GUIDE_GROUP_MEMBER").." %d"..LoadStr("SOCIAL_GROUP_LIST_GROUP_GUIDE_PEOPLE"), tonumber(group["member_num"])));

    self.group_id_to_btn_id_[group["group_id"]] = group_banner_id;
end

function group_guide:get_sub_element_id(name_str, parent_id)
    local ui_str, str_holder = U8toCUIStr(name_str);
    local id = self.uilib_:GetControlIDbyPath(ui_str, parent_id);
    return id;
end

function group_guide:remove_all_group()
    if self.group_list_ctrl_ == nil then
        return;
    end

    while self.group_list_ctrl_:GetItemCount() > 0 do
        self.group_list_ctrl_:RemoveItem(0);
    end
end

function group_guide:click_on_header_or_name(uilib, ctrl_id)
    local sub_ctrl =ui_utility.get_ctrl_ptr_by_id(ctrl_id);
    local p_ctrl = sub_ctrl:GetParent();
    local p_id = p_ctrl:GetControlID();
    local group_id_str = group_guide:btn_id_to_group_id(p_id);
    if group_id_str ~= nil then
        base_utility.log("group_id_str:"..group_id_str);
        local param_table = {group_id = group_id_str};
        event_center.send_event(group_event.SHOW_GROUP_CENTER,base_utility.table_to_json(param_table));
    end
end

function group_guide:click_join_group_btn(uilib, ctrl_id)
    local sub_ctrl =ui_utility.get_ctrl_ptr_by_id(ctrl_id);
    local p_ctrl = sub_ctrl:GetParent();
    local p_id = p_ctrl:GetControlID();

    local group_name = LoadStr("SOCIAL_GROUP_LIST_GROUP_GUIDE_UNKNOW_GROUP");
    local group_name_id = self:get_sub_element_id("Button_Group_Name", p_id);
    local group_name_ptr =ui_utility.get_ctrl_ptr_by_id(group_name_id);
    if group_name_ptr then
        local ui_str = group_name_ptr:GetCaption();
        group_name = CUIStrtoU8(ui_str);
    end


    local id_str_ui = NewStr(200);
    local group_id = group_guide:btn_id_to_group_id(p_id);
    id_str_ui = group_id;
    
    local ext_info = {};
    local from = {};
    from = social_network_control.get_my_remark_info();
    from["gn"] = group_name;
    ext_info["f"] = from;

    local social_network_model =
        base_utility.get_component_adapter("ISocial_network",
            "ierd_tgp.social_network.Get_social_network_adapter();");
    social_network_model:join_group(id_str_ui,base_utility.table_to_json(ext_info));

    --------------------
    local join_group_btn_id = self:get_sub_element_id("Button_JoinGroup", p_id);
   ui_utility.setproperty_by_id(join_group_btn_id, "Visible", "false");

    local already_flag_id = self:get_sub_element_id("TinyStatic_AlreadyFlag", p_id);
   ui_utility.setproperty_by_id(already_flag_id, "Visible", "true");
end

function group_guide:btn_id_to_group_id(btn_id)
    for group_id, id in pairs(self.group_id_to_btn_id_) do
        if btn_id == id then
            return group_id;
        end
    end
end
